package com.mykt.aspect;

import com.mykt.annotation.MyAuthority;
import com.mykt.service.RedisService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;

@Component
@Aspect
public class MyAuthorityHandle {

    @Autowired
    private RedisService redisService;

    public MyAuthorityHandle(){

    }

    @Around("@annotation(com.mykt.annotation.MyAuthority)")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        // 注解鉴权
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        checkMethodAnnotation(method);

        try {
            // 执行原有逻辑
            Object obj = joinPoint.proceed();
            return obj;
        } catch (Throwable e) {
            throw e;
        }

    }


    /**
     * 验证用户是否含有指定权限，只需包含其中一个
     * @param
     */
    public void checkMethodAnnotation(Method method)  {

        MyAuthority myAuthority = method.getAnnotation(MyAuthority.class);

        //@com.mykt.controller.MyAuthority(value=[333])
        if(myAuthority != null){
            //获取权限参数
            String[] value = myAuthority.value();

            //redisService.getCacheObject();

            //CurrentUserUtils.getCurrentUserInfo()

            //读取 用户权限，循环校验
            for(String s:value){
                //权限建议

            }

        }
    }


}
